package com.jiang.readline.Controller;

import com.alibaba.fastjson.JSONObject;
import com.jiang.readline.Service.FileReadCppService;
import com.jiang.readline.Service.FileReadRubyService;
import com.jiang.readline.Service.Impl.FileReadCppServiceImpl;
import com.jiang.readline.Service.Impl.FileReadRubyServiceImpl;
import com.jiang.readline.Utils.ServletUtils;
import com.sun.istack.internal.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;

@RestController
public class FileReadRubyController {
    @Resource
    private FileReadRubyService fileReadRubyService;

    public static String filepathruby;

    @Value("${file.url.ruby}")
    public void setFilepathruby(String filepathruby) {
        FileReadRubyController.filepathruby = filepathruby;
    }
/*
-----------------------------------------------------------
    Files     Lines     Code     Comments      Blanks
-----------------------------------------------------------
    1499     297395     261171     2255       33969
-----------------------------------------------------------
* * */
    //cpp和ruby大体代码相似,只要使用的工具类不同,代码讲解请看cpp
    @RequestMapping(value = "/ReadRuby", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8" })
    public String readRuby(HttpServletRequest request) {
        File file = new File(filepathruby);
        FileReadRubyServiceImpl.RubyFilecount=0;
        FileReadRubyServiceImpl.RubyLines=0;
        FileReadRubyServiceImpl.RubyCode=0;
        FileReadRubyServiceImpl.RubyComments=0;
        FileReadRubyServiceImpl.RubyBlanks=0;
        String answer ;
        String endanswer;
        String requestParams = "";
        try{
            requestParams = ServletUtils.receiveData(request).trim();
            JSONObject reqJso = JSONObject.parseObject(requestParams);
            String value = reqJso.getString("key");
        if("java".equals(value)){
            answer = fileReadRubyService.readFile(file);
            String[] split = answer.split(",");
            int Files = Integer.parseInt(split[0]);
            int Lines =  Integer.parseInt(split[1]);
            int Code =Integer.parseInt(split[2]);
            int Comments = Integer.parseInt(split[3]);
            int Blanks =Integer.parseInt(split[4]);
            endanswer = String.format("-----------------------------------------------------------\n" +
                    "    Files     Lines     Code     Comments      Blanks      \n" +
                    "-----------------------------------------------------------\n" +
                    "    " +"%d"+ "     " +"%d"+ "     "+"%d"+ "     " +"%d"+ "       " +"%d"+ "      " + "\n" +
                    "-----------------------------------------------------------", Files, Lines, Code, Comments,Blanks);

            return  endanswer;
        }else{
            endanswer = String.format("-----------------------------------------------------------\n" +
                    "                         ERROR\n" +
                    "-----------------------------------------------------------\n" +
                    "                         NULL\n" +
                    "-----------------------------------------------------------");
            return  endanswer;
        }
        }catch (Exception e){
            e.printStackTrace();
            return "ReadRuby方法错误";
        }

    }


}
